/**
 * Vue Tools
 * ==============================================================================
 */
Vue.filter('NumberFormat', function (value) {
    if (!value) return 0;
    if (value > 9999) {
        return ('' + value).substring(0, ('' + value).length - 4) + '.' + ('' + value).substring(('' + value).length - 4, ('' + value).length - 3) + 'W';
    } else {
        return value;
    }
})

Vue.filter('Money', function (value) {
    value += '';
    if (!value) return '0.00';

    /*原来用的是Number(value).toFixed(0)，这样取整时有问题，例如0.51取整之后为1*/
    var intPart = Number(value) | 0; // 获取整数部分
    var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); // 将整数部分逢三一断

    var floatPart = ".00"; // 预定义小数部分
    var value2Array = value.split(".");

    // =2表示数据有小数位
    if (value2Array.length == 2) {
        floatPart = value2Array[1].toString(); // 拿到小数部分

        if (floatPart.length == 1) { // 补0,实际上用不着
            return intPartFormat + "." + floatPart + '0';
        } else {
            return intPartFormat + "." + floatPart;
        }

    } else {
        return intPartFormat + floatPart;
    }

})

/**
 * 字典
 */
Vue.filter('Dict', function (value, dictionaries) {
    for (var i in dictionaries) {
        if (value == dictionaries[i].value) {
            return dictionaries[i].name;
        }
    }
})

/**
 * collect/like icon active
 */
Vue.filter('Active', function (value, collections, type, operation) {
    for (var i in collections) {
        if (value == collections[i].dataId && type == collections[i].type && operation == collections[i].operation) {
            return true;
        }
    }
    return false;
})

/**
 * date format
 */
Vue.filter('DateFormat', function (date, fmt) {
    date = new Date(date);
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substring(4 - RegExp.$1.length));
    }
    var o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            var str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substring(str.length));
        }
    }
    return fmt;
});

/**
 * concat upload server and file path
 */
Vue.filter('UploadFile', function (value) {
    return uploadServer + value;
})